﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model IEnumerable<Domain.Dto.UserDto>
@{
    ViewBag.subtitle = "用户管理";
    ViewBag.title = "用户管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("userAddWindow")
        .Title("添加用户")
        .Content(@<text>
    @Html.Partial("_Add", new Domain.Dto.UserDto())
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{Html.Telerik().Grid(Model)
    .Name("UserGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "search"))
        { 
        <label class="customer-label" for="Customers-input">
            用户名搜索:
        </label>
        <input type="text" id="searchUser" value="用户名" data-autocomplete="/user/search" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "add"))
        { 
        <a id="addUser" href="javascript:;" class="t-button">添加用户名</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "muldelete"))
        {
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();

        columns.Bound(o => o.UserName).Width(100).Title("用户名")
        .Template(@<text><a href='/user/edit/@(item.Id)'>@(item.UserName)</a></text>)
        .ClientTemplate("<a href='/user/edit/<#=Id#>'><#=UserName#></a>");

        columns.Bound(o => o.TrueName).Width(100).Title("真实名").Hidden();
        columns.Bound(o => o.Pwd).Width(100).Title("密码").Hidden();

        columns.Bound(o => o.Phone).Width(200).Title("电话");
        columns.Bound(o => o.Email).Width(200).Title("邮箱");
        columns.Bound(o => o.Sex).Width(100).Title("性别");
        columns.Bound(o => o.IpAddress).Width(100).Title("注册IP");
        columns.Bound(o => o.IsActivity).Width(100).Title("是否激活");

        columns.Bound(o => o.UserType).Width(100).Title("用户类型").Hidden();
        columns.Bound(o => o.Guid).Width(300).Title("用户标识").Hidden();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.AddTime).Width(50).Title("添加时间").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();
        columns.Bound(o => o.UserName).Width(100).Title("用户名").Hidden();
        columns.Bound(o => o.Id).Width(100).Title("系统Id").Hidden();
        columns.Bound(o => o.DepartmentID).Width(20).Title("部门Id").Hidden();
        columns.Command(comm =>
        {
            if (Html.HasUiPermission("UserRole", "index"))
            {
                comm.Custom("userRoleCustom")
                    .Text("用户角色")
                    .ButtonType(GridButtonType.Text)
                    .Action("index", "userrole");
            }
            if (Html.HasUiPermission(this.ViewContext, "delete"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(250).Title("角色/删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxindex", "user")
        .Insert("add", "user")
        .Update("edit", "user")
        .Delete("delete", "user"))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#UserGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchUser").val();
        if (searchKeyStr == '用户名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/user/muldelete', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#UserGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }
    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#addUser').bind('click', function (e) {
            e.preventDefault();
            $('#userAddWindow').data('tWindow').center().open();
        });
        $('#checkAllRecords').click(function checkAll() { $("#UserGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            }
            );
        });
        $("#searchUser").focus(function () {
            if (this.value == "用户名") {
                $("#searchUser").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchUser").val("用户名");
            }
        });
    });
</script>
